package com.xxx.shetuan3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.shetuan3.dao.OrganizationDao;
import com.xxx.shetuan3.dao.StudentAndOrganizationDao;
import com.xxx.shetuan3.exception.ApiException;
import com.xxx.shetuan3.pojo.Organization;
import com.xxx.shetuan3.pojo.StudentAndOrganization;
import com.xxx.shetuan3.pojo.User;
import com.xxx.shetuan3.service.StudentAndOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentAndOrganizationServiceImpl implements StudentAndOrganizationService {
    @Autowired
    private StudentAndOrganizationDao studentAndOrganizationDao;
    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public boolean saveStudentAndOrganization(StudentAndOrganization studentAndOrganization) {
        Integer studentId = studentAndOrganization.getStudentId();
        Integer organizationId = studentAndOrganization.getOrganizationId();
        System.out.println("organization id===========>"+organizationId);
        StudentAndOrganization studentAndOrganization1 = studentAndOrganizationDao.findStudentAndOrganizationByStudentIdAndOrganizationId(studentId, organizationId);
        Organization organization1 = organizationDao.findOrganizationById(organizationId);

        if(organization1==null){
            throw new ApiException("社团不存在");
        }
        /*
        Student student1 = studentDao.findStudentById(studentId);
        if(student1 == null){
            throw new ApiException("学生不存在");
        }
         */
        int res = 0;
        if(studentAndOrganization1 != null){
            String state1 = studentAndOrganization1.getState();
            if("审核中".equals(state1)){
                throw new ApiException("正在审核中,请不要重复申请");
            }else if ("已通过".equals(state1)) {
                throw new ApiException("已加入该社团,请不要重复加入");
            }else if ("已拒绝".equals(state1)){
                Integer Id1 = studentAndOrganization1.getOrganizationId();
                res = studentAndOrganizationDao.updateStudentAndOrganizationStateByStudentAndOrganizationId("审核中",Id1);
            }
        }else{
            res = studentAndOrganizationDao.saveStudentAndOrganization(studentAndOrganization);
        }
        if(res == 0){
            throw new ApiException("添加失败");
        }else{
            return true;
        }
    }

    @Override
    public boolean updateStudentAndOrganizationStateByStudentAndOrganizationId(String state, Integer studentAndOrganizationId) {
        StudentAndOrganization studentAndOrganization1 = studentAndOrganizationDao.findStudentAndOrganizationByStudentAndOrganizationId(studentAndOrganizationId);
        if(studentAndOrganization1==null){
            throw new ApiException("studentAndOrganization不存在");
        }else{
            int res = studentAndOrganizationDao.updateStudentAndOrganizationStateByStudentAndOrganizationId(state,studentAndOrganizationId);
            if(res==1){
                return true;
            }else{
                throw new ApiException("更新失败");
            }
        }
    }

    @Override
    public boolean deleteStudentAndOrganizationById(Integer studentAndOrganizationId) {
        StudentAndOrganization studentAndOrganization = studentAndOrganizationDao.findStudentAndOrganizationByStudentAndOrganizationId(studentAndOrganizationId);
        if(studentAndOrganization==null){
            throw new ApiException("StudentAndOrganization不存在");
        }else{
            int res = studentAndOrganizationDao.deleteStudentAndOrganizationById(studentAndOrganizationId);
            if(res==1){
                return true;
            }else{
                throw new ApiException("删除失败");
            }
        }
    }

    @Override
    public List<StudentAndOrganization> findUnacceptedStudent(Integer organizationId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList = studentAndOrganizationDao.findStudentAndOrganizationByStateAndOrganizationId(StudentAndOrganization.STATE1,organizationId);
        PageHelper.startPage(start,size);
        PageInfo pageInfo = new PageInfo(studentAndOrganizationList);
        return pageInfo.getList();
    }

    @Override
    public List<StudentAndOrganization> findStudents(Integer organizationId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList = studentAndOrganizationDao.findStudentAndOrganizationByStateAndOrganizationId(StudentAndOrganization.STATE2,organizationId);
        PageHelper.startPage(start,size);
        PageInfo pageInfo = new PageInfo(studentAndOrganizationList);
        return pageInfo.getList();
    }

    @Override
    public List<StudentAndOrganization> findUnacceptedOrganizations(Integer studentId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList1 = studentAndOrganizationDao.findStudentAndOrganizationByStateAndStudentId(StudentAndOrganization.STATE1,studentId);
        List<StudentAndOrganization> studentAndOrganizationList2 = studentAndOrganizationDao.findStudentAndOrganizationByStateAndStudentId(StudentAndOrganization.STATE3,studentId);
        studentAndOrganizationList1.addAll(studentAndOrganizationList2);
        PageHelper.startPage(start,size);
        PageInfo pageInfo = new PageInfo(studentAndOrganizationList1);
        return pageInfo.getList();
    }

    @Override
    public List<StudentAndOrganization> findOrganizations(Integer studentId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList = studentAndOrganizationDao.findStudentAndOrganizationByStateAndStudentId(StudentAndOrganization.STATE2,studentId);
        PageHelper.startPage(start,size);
        PageInfo pageInfo = new PageInfo(studentAndOrganizationList);
        return pageInfo.getList();
    }

    @Override
    public StudentAndOrganization findStudentAndOrganizationByStudentIdAndOrganizationId(Integer studentId, Integer organizationId) {
        StudentAndOrganization studentAndOrganization = studentAndOrganizationDao.findStudentAndOrganizationByStudentIdAndOrganizationId(studentId, organizationId);
        if(studentAndOrganization==null){
            throw new ApiException("不存在社团关系，无法删除");
        }else{
            return studentAndOrganization;
        }
    }


}
